//
//  TTAccountModel.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/7.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit

let savepath = ((NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!) as NSString).appendingPathComponent("achvie.aaa")

class TTAccountModel: NSObject ,NSCoding{
    //token
    var access_token:String?
    //秒数
    var expires_in:Int = 0
    {
        didSet{
        
        expiresDate = Date().addingTimeInterval(TimeInterval(expires_in))
        }
    }
    //过期时间
    var expiresDate:Date?
    //uid
    var uid:Int64 = 0
    //用户姓名
    var name:String?
    //用户头像
    var avatar_large:String?
    
    //重载方法转模型
    init(dict:[String:Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        //空实现
    }
    
    //  MARK:   --归档和解档
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expiresDate, forKey: "expiresDate")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(name, forKey: "name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
    }
    
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expiresDate = aDecoder.decodeObject(forKey: "expiresDate") as? Date
        uid = aDecoder.decodeInt64(forKey: "uid")
        name = aDecoder.decodeObject(forKey: "name") as? String
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
    }
    
    //归档方法
    func saveUserAccount()
    {
       
    NSKeyedArchiver.archiveRootObject(self, toFile: savepath)
        
    }
    
   class func solutionUserAccount()->TTAccountModel?
    {
     let account =  NSKeyedUnarchiver.unarchiveObject(withFile: savepath) as? TTAccountModel
    
        return account
    }
}
